﻿#include <stdio.h>
/* #include <conio.h> */
#include <string.h>
#include <stdlib.h>
#include <time.h>


typedef enum
{
    CONTEX_TYPE_END = -1,
    CONTEX_TYPE_NONE = 0,
    CONTEX_TYPE_FILE,
    CONTEX_TYPE_BRIEF,
    CONTEX_TYPE_AUTHOR,
    CONTEX_TYPE_DATE,
    CONTEX_TYPE_VERSION,
    CONTEX_TYPE_INCLUDE,
    CONTEX_TYPE_DEFINE_H1,
    CONTEX_TYPE_DEFINE_H2

}CONTEX_TYPE_t;

typedef struct
{
    CONTEX_TYPE_t line_type;
    char line_contex[256];
}file_info_t;

file_info_t cFile_info[100] =
{
    {0,                         {"/***************************************************************************"},},
    {CONTEX_TYPE_FILE,          {"** @file:     "                                                               },},
    {CONTEX_TYPE_BRIEF,         {"** @brief:    "                                                               },},
    {CONTEX_TYPE_AUTHOR,        {"** @author:   "                                                               },},
    {CONTEX_TYPE_DATE,          {"** @date:     "                                                               },},
    {CONTEX_TYPE_VERSION,       {"** @version:  "                                                               },},
    {0,                         {"***************************************************************************/"},},
    {CONTEX_TYPE_INCLUDE,       {"#include \""                                                                    },},
    {0,                         {""                                                                               },},
    {0,                         {""                                                                               },},
    {0,                         {""                                                                               },},
    {CONTEX_TYPE_END,           {"/*****************End of file "                                              },},
    {0,                         {""                                                                            },},
};

file_info_t hFile_info[100] =
{
    {0,                         {"/***************************************************************************"},},
    {CONTEX_TYPE_FILE,          {"** @file:     "                                                               },},
    {CONTEX_TYPE_BRIEF,         {"** @brief:    "                                                               },},
    {CONTEX_TYPE_AUTHOR,        {"** @author:   "                                                               },},
    {CONTEX_TYPE_DATE,          {"** @date:     "                                                               },},
    {CONTEX_TYPE_VERSION,       {"** @version:  "                                                               },},
    {0,                         {"***************************************************************************/"},},
    {CONTEX_TYPE_DEFINE_H1,     {"#ifndef "                                                                       },},
    {CONTEX_TYPE_DEFINE_H2,     {"#define "                                                                       },},
    {0,                         {""                                                                               },},
    {0,                         {""                                                                               },},
    {0,                         {""                                                                               },},
    {CONTEX_TYPE_END,           {"#endif /* "                                                                  },},
    {0,                         {""                                                                            },},
};

#define FILE_TYPE_NONE 0
#define FILE_TYPE_C  1
#define FILE_TYPE_H  2


int main(int argc,void** argv)
{
    FILE *fp;
    int ch;
    int file_type = 0;  // 0-NONE  1-cFile 2-hFile
    char file_name[50] ="";
    char hfile_header[54] ="";
    file_info_t *p_file_info;

    if (argc != 2)
    {
        printf("Usage:create_file filename");
        return 0;
    }
    else
    {
        strncpy(file_name,argv[1],50);
    }


    if ( (file_name[strlen(file_name)-1]=='h') &&  (file_name[strlen(file_name)-2]=='.'))
    {
        file_type = FILE_TYPE_H;

        strcat(hfile_header,"__");
        for(int i = 0;i<strlen(file_name);i++)
        {
            if(file_name[i]=='.')
            {
                hfile_header[i+2] = '_';
            }

            else if( file_name[i]>='a' &&  file_name[i]<='z')
            {
                hfile_header[i+2] = file_name[i] - 32; // uppercase
            }
            else
            {
                hfile_header[i+2] = file_name[i];
            }

        }
        strcat(hfile_header,"__");

        p_file_info = hFile_info;
    }
    else
    {
        file_type = FILE_TYPE_C;
        p_file_info = cFile_info;

    }
    //// "abc.c"  '.'位置在stlen("abc.c")-2上
    //file_type = FILE_TYPE_NONE;
    //if ( (file_name[strlen(file_name)-1]=='c') &&  (file_name[strlen(file_name)-2]=='.'))
    //{
    //    file_type = FILE_TYPE_C;
    //    p_file_info = cFile_info;
    //}



    if(file_type==FILE_TYPE_NONE)
    {
        printf("Usage:create_file filename.c or filename.h");
        return 0;
    }


    // 以追加方式打开
    if((fp = fopen(file_name,"a+"))==NULL )
    {

        printf("Cannot open %s\n",file_name);
        exit(0);
    }

    // 判断文件是否为空
    {
        fgetc(fp);
        if(feof(fp))
        {
            int exit_for_flag = 0;
            int i = 0;
            // 添加文件内容
            for (i = 0;i<100;i++)
            {

                switch(p_file_info[i].line_type)
                {
                    case CONTEX_TYPE_END:
                        exit_for_flag = 1;
                        if(file_type == FILE_TYPE_C)
                        {
                            strcat(p_file_info[i].line_contex,file_name);
                            strcat(p_file_info[i].line_contex,"***********/");
                        }
                        else if(file_type == FILE_TYPE_H)
                        {
                            strcat(p_file_info[i].line_contex,hfile_header);
                            strcat(p_file_info[i].line_contex," */");
                        }

                        break;
                    case CONTEX_TYPE_FILE:
                        strcat(p_file_info[i].line_contex,file_name);

                        break;

                    case CONTEX_TYPE_AUTHOR:
                        break;

                    case CONTEX_TYPE_DATE:
                        // get time
                        #if 0
                        {
                            time_t curtime;
                            time(&curtime);
                            strcat(p_file_info[i].line_contex,ctime(&curtime));
                            p_file_info[i].line_contex[strlen(p_file_info[i].line_contex)-1]='\0'; // 将倒数第二个字符(换行符)改为结束符
                        }
                        #endif

                        {

                                time_t rawtime;
                                struct tm *info;
                                char buf[20];
                                //struct tm {
                                //   int tm_sec;         /* 秒，范围从 0 到 59                */
                                //   int tm_min;         /* 分，范围从 0 到 59                */
                                //   int tm_hour;        /* 小时，范围从 0 到 23                */
                                //   int tm_mday;        /* 一月中的第几天，范围从 1 到 31                    */
                                //   int tm_mon;         /* 月份，范围从 0 到 11                */
                                //   int tm_year;        /* 自 1900 起的年数                */
                                //   int tm_wday;        /* 一周中的第几天，范围从 0 到 6                */
                                //   int tm_yday;        /* 一年中的第几天，范围从 0 到 365                    */
                                //   int tm_isdst;       /* 夏令时                        */
                                //};

                                time( &rawtime );
                                info = localtime( &rawtime );
                                sprintf(buf, "%04d-%02d-%02d", 1900+info->tm_year,1+info->tm_mon,info->tm_mday);
                                  strcat(p_file_info[i].line_contex,buf);

                        }

                        break;

                    case CONTEX_TYPE_VERSION:
                        break;

                    case CONTEX_TYPE_INCLUDE:
                    {
                        char tmp_file_name[50] ="";
                        strcpy(tmp_file_name,file_name);
                        /* 将xxx.yy替换为xxx.h */
                        /* 1 查找'.'的位置*/
                        char *ret = strstr(tmp_file_name, "."); /*ret = .yy*/
                        ret[1] = 'h';
                        ret[2] = '\"';
                        ret[3] = '\0';

                        strcat(p_file_info[i].line_contex,tmp_file_name);
                    }
                    break;

                    case CONTEX_TYPE_DEFINE_H1:
                    case CONTEX_TYPE_DEFINE_H2:
                        strcat(p_file_info[i].line_contex,hfile_header);
                        break;

                    default:
                        ;

                }

                fprintf(fp,"%s\n",p_file_info[i].line_contex);
                if(exit_for_flag!=0) break;

            }
            printf("File %s create successed.\n",file_name);
        }
        else
        {
            printf("File is exist.\n");
            /* seek to the beginning of the file */
            fseek(fp, 0, SEEK_SET);
            do
            {
                /* read a char from the file */
                ch = fgetc(fp);
                /* display the character */
                putchar(ch);
            }while (ch != EOF);
        }
    }

    fclose(fp);

    printf("Press any key to exit.\n");
    getchar();
    return 0;
}
